cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //    default: null,      // The default value will be used only when the component attaching
        //                           to a node for the first time
        //    url: cc.Texture2D,  // optional, default is typeof default
        //    serializable: true, // optional, default is true
        //    visible: true,      // optional, default is true
        //    displayName: 'Foo', // optional
        //    readonly: false,    // optional, default is false
        // },
        // ...
    },

    // use this for initialization
    onLoad: function () {
        this.defaultGid = 1;
        this.startPos = null;
        this.endPos = null;
        this.tiledMap = this.node.getComponent(cc.TiledMap);

        this.tileMapSize = this.tiledMap.getMapSize();
        this.tileArray = new Array();
        var layer = this.tiledMap.getLayer("grid");
        for(var i = 0;i<this.tileMapSize.width;i++){
            this.tileArray[i] = new Array();
             for(var j =0;j<this.tileMapSize.height;j++){
                var curGID = layer.getTileGIDAt(cc.p(i,j));
                this.tileArray[i][j] = 1;
            }           
        }

    },

    //处理点击事件
    clickEvent:function(pos){
        //坐标转换
        var clickedPos = this.node.convertToNodeSpace(pos);
        this.getTilePosByGlPos(clickedPos);
    },

     //opengl坐标 转换为tiled坐标
    getTilePosByGlPos:function(glPos){
        var mapSize = this.node.getContentSize();
        var tileSize = this.tiledMap.getTileSize();
        var xPos = glPos.x;
        var yPos = mapSize.height - glPos.y;
        //矩形
        var grid_x = Math.floor(xPos/tileSize.width);
        var grid_y = Math.floor(yPos/tileSize.height);

        this.onClickGrid(cc.p(grid_x,grid_y));
    },

    runFindPath:function(){ 
        if(!this.startPos){
            this.showTips("请设置起始点")
            return
        }
        if(!this.endPos){
            this.showTips("请设置终点")
            return
        }
        //寻路
        //根据 不同的选择 调用不同的寻路方法 
        //默认 a* 
        if(!this.method){
            var Astar = require("AStar")
            this.method = new Astar()
        }
        var path = this.method.findPath(this.startPos,this.endPos,this.tileMapSize,this.tileArray);
        //高亮显示 路径点
        console.log("path:",path.toString());
        this.showPath(path);
    },

    //清除所有的颜色块
    clearAllBlock:function(){
        var tileMapSize = this.tiledMap.getMapSize();
        var layer = this.tiledMap.getLayer("grid");
        for(var i=0;i<tileMapSize.width;i++)
            for(var j=0;j<tileMapSize.height;j++){
                layer.setTileGID(this.defaultGid,cc.p(i,j));
                this.tileArray[i][j]=1;
            }

        this.startPos = null;
        this.endPos = null;
    },

    //更新tile色块
    onClickGrid:function(pos){
        var layer = this.tiledMap.getLayer("grid");
        var curGID = layer.getTileGIDAt(pos);
        if(curGID+1>4){
            curGID = 1;
        }else{
            curGID +=1;
        }
        if(curGID==3){
            if(this.startPos){
                this.onUpdateGrid(this.startPos,this.defaultGid)
            }
            this.startPos = pos;
        }else if(curGID==4){
            if(this.endPos){
                this.onUpdateGrid(this.endPos,this.defaultGid);
            }
            this.endPos = pos;    
            if(this.startPos.x==this.endPos.x && this.startPos.y==this.endPos.y){
                this.startPos = null
            }       
        }
        this.onUpdateGrid(pos,curGID);
    },

    onUpdateGrid:function(pos,curGID){
        var layer = this.tiledMap.getLayer("grid");
        layer.setTileGID(curGID,pos)
        this.tileArray[pos.x][pos.y] = curGID    
    },

    showPath:function(posArr){
        var layer = this.tiledMap.getLayer("grid");
        for(var i=0;i<posArr.length;i++){
            var pos = posArr[i]
            layer.setTileGID(this.defaultGid+4,pos)
        }
    },

    //操作提示
    showTips:function(msg){
        //直接添加
        var defaultHeight = 120;
        var label = new cc.Node().addComponent(cc.Label);
        label.string = msg;
        label.node.color = new cc.Color(255,10,0);
        label.node.setPosition(0,0);
        label.node.parent = this.node;
        label.node.runAction(cc.sequence(cc.moveTo(0.5,cc.p(0,80)),
                cc.callFunc(function(){
                    label.node.removeFromParent();
                    }, this)));
    }
    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
